<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>二维码统计</title>
    <meta name="viewport" content="width=1000px,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <link rel="stylesheet" href="<?php echo C('BOOTSTRAP_CSS');?>css/bootstrap.min.css">
    <style>
        th,tr{
            text-align:center
        }
    </style>
</head>
<body>
    <div class="container-full">
            <div class="col-lg-12">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th colspan="8">二维码推广统计</th>
                    </tr>
                    <tr>
                        <th colspan="8"><button type="button" class="btn btn-danger" style="float:right" id="add">添加新的数据</button> </th>
                    </tr>
                    <tr>
                        <th>自增ID</th><th>场景ID</th><th>渠道名称</th><th>所属公众号</th><th>联系方式</th><th>拨打</th><th>扫描次数</th><th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if(is_array($qrcode)): foreach($qrcode as $key=>$qrcode): ?><tr>
                            <td id="id"><?php echo ($qrcode['id']); ?></td>
                            <td id="s_id"><?php echo ($qrcode['s_id']); ?></td>
                            <td id="s_name"><?php echo ($qrcode['s_name']); ?></td>
                            <td id="s_weixin"><?php echo ($qrcode['open_weixin']); ?></td>
                            <td id="s_phone"> <?php echo ($qrcode['s_phone']); ?></td>
                            <td id="s_phone_call"><button id="call" type="button" class="btn btn-info" date-tel="<?php echo ($qrcode['s_phone']); ?>">点击拨打</button></td>
                            <td id="s_c"><?php echo ($qrcode['s_c']); ?></td>
                            <td>
                                <button type='button' class='btn btn-danger' id='update'>更新数据</button>
                                <button type='button' class='btn btn-danger' style='display: none'  id='update-button'>确认更新</button>
                                <button type='button' class='btn btn-info' id="download">下载二维码</button>
                            </td>
                        </tr><?php endforeach; endif; ?>
                    <tr class="add-line"></tr>
                    </tbody>
                </table>
            </div>
        </div>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(function(){
            $('button[id=call]').click(function(){
                var tel = $(this).attr('date-tel');
                window.open('tel://'+tel);
            })
            $('button[id=download]').click(function(){
                var s_id = $(this).parent().parent().find('td[id=s_id]').html();
                var s_weixin = $(this).parent().parent().find('td[id=s_weixin]').html();
                window.open('<?php echo U("/'+s_weixin+'/Index/qrCode");?>/?s_id='+s_id);
            })
            id = prompt("请输入你的ID:","");
            pwd = prompt("请输入你的密码","");
            if (id != null && pwd != null){
                $.ajax({
                    url:'<?php echo U("/Home/Index/userAuth");?>',
                    type:'POST',
                    data:{id:id,pwd:pwd},
                    dataType:'text',
                    success:function(data){
                        if(data == '1'){
                        alert('你验证通过了');
                        }else{
                            window.open('http://www.baidu.com/','_self');
                        }
                    }
                })
            }else{
                window.open('http://www.baidu.com/','_self');
            }
            $('button[id=update]').click(function(){
                $(this).parent().find('button[id=update-button]').show() && $(this).hide();
                var tr = $(this).parent().parent();
                var s_id = tr.find('td[id=s_id]').html();
                var s_name = tr.find('td[id=s_name]').html();
                var s_c = tr.find('td[id=s_c]').html();
                var s_weixin = tr.find('td[id=s_weixin]').html();
                var s_phone = tr.find('td[id=s_phone]').html();
                tr.find('td[id=s_id]').html('<input type="text" name="s_id" value="'+s_id+'" class="input">');
                tr.find('td[id=s_name]').html('<input type="text" name="s_name" value="'+s_name+'" class="input">');
                tr.find('td[id=s_c]').html('<input type="text" name="s_c" value="'+s_c+'" class="input">');
                tr.find('td[id=s_weixin]').html('<input type="text" name="s_weixin" value="'+s_weixin+'" class="input">');
                tr.find('td[id=s_phone]').html('<input type="text" name="s_phone" value="'+s_phone+'" class="input">');
                $('button[id=update-button]').click(function(){
                    $(this).parent().find('button[id=update]').show() && $(this).hide();
                    var tr = $(this).parent().parent();
                    var id = tr.find('td[id=id]').html();
                    var s_id = tr.find('input[name=s_id]').val();
                    var s_name = tr.find('input[name=s_name]').val();
                    var s_c = tr.find('input[name=s_c]').val();
                    var s_weixin = tr.find('input[name=s_weixin]').val();
                    var s_phone = tr.find('input[name=s_phone]').val();
                    $.ajax({
                        url:'<?php echo U("/Home/Index/qrcodeUpdate");?>',
                        type:'POST',
                        data:{id:id,s_id:s_id,s_name:s_name,s_c:s_c,s_weixin:s_weixin,s_phone:s_phone},
                        dataType:'text',
                        success:function(data){
                            if(data == '1'){
                                tr.find('td[id=s_id]').html(s_id);
                                tr.find('td[id=s_name]').html(s_name);
                                tr.find('td[id=s_c]').html(s_c);
                                tr.find('td[id=s_weixin]').html(s_weixin);
                                tr.find('td[id=s_phone]').html(s_phone);
                                tr.find('td[id=s_phone_call]').find('#call').attr('date-tel',s_phone);
                            }else{
                                alert('请注意，出现这个错误的原因是因为你的数据无更新,也可能是误报，你可以刷新页面试试');
                            }
                        }
                    })
                })
            })
            $('#add').click(function(){
                var tpl = "<tr id='remove'>" +
                          "<th scope='row'>#</th>" +
                          "<td id='s_id'><input type='text' name='s_id' value='' class='input'></td>" +
                          "<td id='s_name'><input type='text' name='s_name' value='' class='input'></td>" +
                        "<td id='s_weixin'><input type='text' name='s_weixin' value='' class='input'></td>" +
                        "<td id='s_phone'><input type='text' name='s_phone' value='' class='input'></td>" +
                        "<td id='s_c'><input type='text' name='s_c' value='' class='input'></td>" +
                          "<td colspan='2'>" +
                          "<button type='button' class='btn btn-danger'  style='display: none'  id='update'>更新数据</button>" +
                          "<button type='button' class='btn btn-danger' id='update-button'>确认更新</button>" +
                          "<button type='button' class='btn btn-info'>下载二维码</button>" +
                          "</td>" +
                          "</tr>";
                $('.add-line').before(tpl);
                $('button[id=update-button]').click(function(){
                    var tr = $(this).parent().parent();
                    var s_id = tr.find('input[name=s_id]').val();
                    var s_name = tr.find('input[name=s_name]').val();
                    var s_c = tr.find('input[name=s_c]').val();
                    var s_weixin = tr.find('input[name=s_weixin]').val();
                    var s_phone = tr.find('input[name=s_phone]').val();
                    $.ajax({
                        url:'<?php echo U("/Home/Index/qrcodeAdd");?>',
                        type:'POST',
                        data:{s_id:s_id,s_name:s_name,s_c:s_c,s_weixin:s_weixin,s_phone:s_phone},
                        dataType:'text',
                        success:function(data){
                            if(data == '1'){
                                $('tr[id=remove]').remove();
                                var tpl = "<tr>" +
                                        "<th scope='row'>#</th>" +
                                        "<td id='s_id'>"+s_id+"</td>" +
                                        "<td id='s_name'>"+s_name+"</td>" +
                                        "<td id='s_weixin'>"+s_weixin+"</td>" +
                                        "<td id='s_phone'>"+s_phone+"</td>" +
                                        "<td id='s_phone_call'><button id='call' type='button' class='btn btn-info' date-tel="+s_phone+">点击拨打</button></td>" +
                                        "<td id='s_c'>"+s_c+"</td>" +
                                        "<td colspan='2'>" +
                                        "<button type='button' class='btn btn-danger' id='update'>更新数据</button>" +
                                        "<button type='button' class='btn btn-danger' style='display: none'  id='update-button'>确认更新</button>" +
                                        "<button type='button' class='btn btn-info'>下载二维码</button>" +
                                        "</td>" +
                                        "</tr>";
                                $('.add-line').before(tpl);
                            }else{
                                alert('请注意，出现这个错误的原因不在你的操作上，而在于程序上，请联系我！');
                            }
                        }
                    })
                })
            })
        })
    </script>
</body>
</html>